package com.ctshk.rpc.order.season.service.impl;

import com.alibaba.fastjson.JSON;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SeasonIndividualType;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.SeasonOrderManager;
import com.ctshk.rpc.order.season.dto.*;
import com.ctshk.rpc.order.season.req.DeleteDynamicProductReq;
import com.ctshk.rpc.order.season.req.DynamicProductReq;
import com.ctshk.rpc.order.season.req.TouristTypeReq;
import com.ctshk.rpc.order.season.service.ISeasonReceiptService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author  杨林
 * @date  2021/4/6 10:27
 * @version 1.0
 */

@DubboService
public class SeasonReceiptServiceImpl implements ISeasonReceiptService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result<DynamicProductCartDTO> getCartList(TokenUser user) {
        String cartKey= SeasonOrderManager.USER_DYNAMIC_PRODUCT_TOKEN_KEY+user.getId();
        BoundHashOperations<String, Object, Object> cartOps = redisTemplate.boundHashOps(cartKey);
        DynamicProductCartDTO dto=new DynamicProductCartDTO();
        List<String> seasonPackageProductFlagList=new ArrayList<>();
        List<TouristTypeReq> touristList=new LinkedList<>();
        BigDecimal totalDynamicPrice= BigDecimal.ZERO;
        for(Map.Entry entry:cartOps.entries().entrySet()){
            String productFlag=String.valueOf(entry.getKey());
            seasonPackageProductFlagList.add(productFlag);
            Map<String,Object> md=JSON.parseObject(String.valueOf(entry.getValue()));
            List<TouristTypeReq> typeList= (List<TouristTypeReq>) md.get("touristTypeReqList");
            if(CollectionUtils.isNotEmpty(typeList)){
                touristList.addAll(typeList);
            }

            Object obj=md.get("productInfoList");
            //1:机票 2:酒店 3:巴士 4:船票 5:火车票 6:景点门票 7:当地玩乐
            SeasonIndividualType productFlagEnum = SeasonIndividualType.getEnumType(productFlag);
            switch (productFlagEnum) {
                case AIR:
                    List<SeasonProductOrderAirDTO> airList=(List<SeasonProductOrderAirDTO>) obj;
//                    double airPrice= airList.stream().mapToDouble(SeasonProductOrderAirDTO::getProductPrice).sum();
 //                  System.out.println(airPrice);
//                    totalDynamicPrice.add(airPrice);
                    dto.setAirList(airList);
                    break;
                case HOTEL:
                    List<SeasonProductOrderHotelDTO> hotelList=(List<SeasonProductOrderHotelDTO>) obj;
//                    BigDecimal hotelPrice=hotelList.stream().map(SeasonProductOrderHotelDTO::getProductPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
//                    totalDynamicPrice.add(hotelPrice);
                    dto.setHotelList(hotelList);
                    break;
                case BUS:
                    List<SeasonProductOrderBusDTO> busList=(List<SeasonProductOrderBusDTO>) obj;
//                    BigDecimal busPrice=busList.stream().map(SeasonProductOrderBusDTO::getProductPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
//                    totalDynamicPrice.add(busPrice);
                    dto.setBusList(busList);
                    break;
                case SHIP:
                    List<SeasonProductOrderShipDTO> shipList=(List<SeasonProductOrderShipDTO>) obj;
                    BigDecimal shipPrice=shipList.stream().map(SeasonProductOrderShipDTO::getProductPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
//                    totalDynamicPrice.add(shipPrice);
                    dto.setShipList(shipList);
                    break;
                case TRAIN:
                    List<SeasonProductOrderTrainDTO> trainList=(List<SeasonProductOrderTrainDTO>) obj;
//                    BigDecimal trainPrice=trainList.stream().map(SeasonProductOrderTrainDTO::getProductPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
//                    totalDynamicPrice.add(trainPrice);
                    dto.setTrainList(trainList);
                    break;
                case SCENIC:
                    List<SeasonProductOrderScenicDTO> scenicList=(List<SeasonProductOrderScenicDTO>) obj;
//                    BigDecimal scenicPrice=scenicList.stream().map(SeasonProductOrderScenicDTO::getProductPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
//                    totalDynamicPrice.add(scenicPrice);
                    dto.setScenicList(scenicList);
                    break;
                case LOCALFUN:
                    List<SeasonProductOrderLocalFunDTO> localFunList=(List<SeasonProductOrderLocalFunDTO>) obj;
//                    BigDecimal localFunPrice=localFunList.stream().map(SeasonProductOrderLocalFunDTO::getProductPrice).reduce(BigDecimal.ZERO,BigDecimal::add);
//                    totalDynamicPrice.add(localFunPrice);
                    dto.setLocalFunList(localFunList);
                    break;
            }

        }

        dto.setTotalDynamicPrice(totalDynamicPrice);

        return Result.success(dto);
    }

    
    /* @MethodName:动态套票  添加购物车
     * @Description: TODO
     * @Param: 
     * @Return: 
     * @Author: 杨林
     * @Date:2021/4/6 10:46
    **/
    @Override
    public Result addProductCart(TokenUser user, DynamicProductReq req) {
        Long userId=user.getId();
        //获取购物车选项
        BoundHashOperations<String, Object, Object> cartOps=getCartOps(userId);
        //商品标识 1:机票 2:酒店 3:巴士 4:船票 5:火车票 6:景点门票 7:当地玩乐"
        String productFlag=req.getSeasonProductFlag();
        SeasonIndividualType productFlagEnum = SeasonIndividualType.getEnumType(productFlag);
        Map<String,Object> addMap=new HashedMap();

        addMap.put("touristTypeReqList",req.getTouristTypeReqList());
        switch (productFlagEnum){
            case AIR:
                addMap.put("productInfoList",req.getAirList());
                break;
            case HOTEL:
                addMap.put("productInfoList",req.getHotelList());
                break;
            case BUS:
                addMap.put("productInfoList",req.getBusList());
                break;
            case SHIP:
                addMap.put("productInfoList",req.getAirList());
                break;
            case TRAIN:
                addMap.put("productInfoList",req.getTrainList());
                break;
            case SCENIC:
                addMap.put("productInfoList",req.getScenicList());
                break;
            case LOCALFUN:
                addMap.put("productInfoList",req.getLocalFunList());
                break;
            default:
                addMap.put("productInfoList",new ArrayList<>());
        }
        cartOps.put(productFlag, JSON.toJSONString(addMap));
        return Result.success();
    }

    /* @MethodName: 删除
     * @Description: TODO
     * @Param: 
     * @Return: 
     * @Author: 杨林
     * @Date:2021/4/6 16:52
    **/
    @Override
    public Result deleteProductCart(TokenUser user, DeleteDynamicProductReq req) {
        BoundHashOperations<String, Object, Object>  ops=getCartOps(user.getId());
                ops.delete(req.getSeasonProductFlag());
        return Result.success();
    }

    /* @MethodName: 批量删除
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: Eric
     * @Date:2021/4/16 18:33
     **/
    @Override
    public Result deleteBatchProductCart(TokenUser user, List<DeleteDynamicProductReq> reqs) {
        reqs.forEach(req -> {
            BoundHashOperations<String, Object, Object> ops = getCartOps(user.getId());
            ops.delete(req.getSeasonProductFlag());
        });
        return Result.success();
    }
    /* @MethodName: 获取购物车选项
     * @Description: TODO
     * @Param: 
     * @Return: 
     * @Author: 杨林
     * @Date:2021/4/6 10:50
    **/
    private BoundHashOperations<String, Object, Object> getCartOps(Long userId) {
        String cartKey= SeasonOrderManager.USER_DYNAMIC_PRODUCT_TOKEN_KEY+userId;
        BoundHashOperations<String, Object, Object> ops=redisTemplate.boundHashOps(cartKey);
        return  ops;
    }




}